package com.example.callloggenerator;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private EditText etNumber, etDuration;
    private NumberPicker npYear, npMonth, npDay, npHour, npMinute;
    private Spinner spType;
    private Button btnGenerate;
    private CallLogGenerator callLogGenerator;
    private PermissionHelper permissionHelper;

    private static final long DEFAULT_DURATION = 60L; // 默认通话时长为60秒
    private static final String DEFAULT_NUMBER = "13737377391"; // 默认电话号码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        initViews();

        // 设置NumberPicker的范围
        setupNumberPickers();

        // 设置Spinner的适配器
        setupSpinnerAdapter();

        callLogGenerator = new CallLogGenerator(this);

        // 设置单独生成通话记录按钮的点击事件
        findViewById(R.id.btnGenerate).setOnClickListener(view ->{
//            CallLogData callData = collectUserInput();
            CallLogData callData = collectUserInputForSingle();
            if (callData != null){
                callLogGenerator.generateCallLogs(callData);
            }
        } );

        // 设置批量生成通话记录按钮的点击事件
        findViewById(R.id.btnBatchGenerate).setOnClickListener(view ->{
            CallLogData callData = collectUserInputForBatch();
            if (callData != null){
                callLogGenerator.batchGenerateCallLogs(callData);
            }
        } );

        // 设置批量生成400通话记录按钮的点击事件
        findViewById(R.id.btnBatchGenerate400).setOnClickListener(view ->{
            CallLogData callData = collectUserInputForBatch();
            if (callData != null){
                callLogGenerator.batchGenerateCall400Logs(callData);
            }
        } );


        // 初始化辅助类
        callLogGenerator = new CallLogGenerator(this);
        permissionHelper = new PermissionHelper(this);

        // 检查权限
        permissionHelper.checkPermissions();
    }

    private void initViews() {
        etNumber = findViewById(R.id.etNumber);
        spType = findViewById(R.id.spType);
        npYear = findViewById(R.id.npYear);
        npMonth = findViewById(R.id.npMonth);
        npDay = findViewById(R.id.npDay);
        npHour = findViewById(R.id.npHour);
        npMinute = findViewById(R.id.npMinute);
        etDuration = findViewById(R.id.etDuration);
        btnGenerate = findViewById(R.id.btnGenerate);
    }

    private void setupNumberPickers() {
        // 简化的设置NumberPicker的范围和初始值
        Calendar calendar = Calendar.getInstance();
        npYear.setMinValue(1900);
        npYear.setMaxValue(calendar.get(Calendar.YEAR));
        npYear.setValue(npYear.getMaxValue());

        npMonth.setMinValue(1);
        npMonth.setMaxValue(12);
        npMonth.setValue(calendar.get(Calendar.MONTH) + 1);

        npDay.setMinValue(1);
        npDay.setMaxValue(31);
        npDay.setValue(calendar.get(Calendar.DAY_OF_MONTH));

        npHour.setMinValue(0);
        npHour.setMaxValue(23);
        npHour.setValue(calendar.get(Calendar.HOUR_OF_DAY));

        npMinute.setMinValue(0);
        npMinute.setMaxValue(59);
        npMinute.setValue(calendar.get(Calendar.MINUTE));
    }

    //自定义类来封装通话记录所需的数据
    public class CallLogData {
        private final String number;
        private final long duration;
        private final Calendar calendar;
        private final int type;

        public CallLogData(String number, long duration, Calendar calendar, int type) {
            this.number = number;
            this.duration = duration;
            this.calendar = calendar;
            this.type = type;
        }

        // Getters
        public String getNumber() {
            return number;
        }

        public long getDuration() {
            return duration;
        }

        public Calendar getCalendar() {
            return calendar;
        }

        public int getType() {
            return type;
        }
    }

    // 用于收集单条通话记录的数据并验证输入
    private CallLogData collectUserInputForSingle() {
        // 收集用户输入的数据
        String number = etNumber.getText().toString();
        int year = npYear.getValue();
        int month = npMonth.getValue() - 1; // NumberPicker从1开始，而月份是从0开始
        int day = npDay.getValue();
        int hour = npHour.getValue();
        int minute = npMinute.getValue();
        String durationStr = etDuration.getText().toString();
        int type = spType.getSelectedItemPosition() + 1; // 1=INCOMING, 2=OUTGOING, 3=MISSED

        if (TextUtils.isEmpty(number) || TextUtils.isEmpty(durationStr)) {
            Toast.makeText(this, "请输入所有字段", Toast.LENGTH_SHORT).show();
            return null;
        }

        long duration = Long.parseLong(durationStr);

        // 创建一个Calendar实例并设置日期和时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, 0); // 注意：月份从0开始

        return new CallLogData(number, duration, calendar, type);
    }

    // 用于收集批量生成通话记录的数据，不做输入验证
    private CallLogData collectUserInputForBatch() {
        // 收集用户输入的数据，默认值可用于未填写的字段
        String number = etNumber.getText().toString();
        int year = npYear.getValue();
        int month = npMonth.getValue() - 1; // NumberPicker从1开始，而月份是从0开始
        int day = npDay.getValue();
        int hour = npHour.getValue();
        int minute = npMinute.getValue();
        String durationStr = etDuration.getText().toString();
        int type = spType.getSelectedItemPosition() + 1; // 1=INCOMING, 2=OUTGOING, 3=MISSED

        long duration = TextUtils.isEmpty(durationStr) ? DEFAULT_DURATION : Long.parseLong(durationStr);

        // 创建一个Calendar实例并设置日期和时间
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, 0); // 注意：月份从0开始

        return new CallLogData(number, duration, calendar, type);
    }

//    // 收集用户的输入
//    private CallLogData collectUserInput(){
//        // 收集用户输入的数据
//        String number = etNumber.getText().toString();
//        int year = npYear.getValue();
//        int month = npMonth.getValue() - 1; // NumberPicker从1开始，而月份是从0开始
//        int day = npDay.getValue();
//        int hour = npHour.getValue();
//        int minute = npMinute.getValue();
//        String durationStr = etDuration.getText().toString();
//        int type = spType.getSelectedItemPosition() + 1; // 1=INCOMING, 2=OUTGOING, 3=MISSED
//
//        if (TextUtils.isEmpty(number) || TextUtils.isEmpty(durationStr)) {
//            Toast.makeText(this, "请输入所有字段", Toast.LENGTH_SHORT).show();
//            return null;
//        }
//
//        long duration = Long.parseLong(durationStr);
//
//        // 创建一个Calendar实例并设置日期和时间
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(year, month, day, hour, minute, 0); // 注意：月份从0开始
//
//        return new CallLogData(number, duration,  calendar, type);
//
//    }

    private void setupSpinnerAdapter() {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.call_types, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spType.setAdapter(adapter);
        spType.setOnItemSelectedListener(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // 可以在这里处理选择事件
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // 可以在这里处理未选择事件
    }

}